#include "histogramyuv.h"

HistogramYUV::HistogramYUV():Histogram()
{
    histogramY=new unsigned int[256];
    histogramU=new unsigned int[256];
    histogramV=new unsigned int[256];
}
HistogramYUV::HistogramYUV(QImage *im):Histogram(im)
{
    histogramY=new unsigned int[256];
    histogramU=new unsigned int[256];
    histogramV=new unsigned int[256];
}

HistogramYUV::~HistogramYUV()
{
    delete [] histogramY;
    delete [] histogramU;
    delete [] histogramV;
}

void HistogramYUV::calcCDF()
{
    return;
}

void HistogramYUV::calcHistogram()
{
    if(ownerImage==NULL)
    {
        return;
    }
    else
    {
        for(int i=0;i<256;i++)
        {
            histogramY[i]=0;
            histogramU[i]=0;
            histogramV[i]=0;
        }
        for(int x=0;x<ownerImage->width();x++)
            for(int y=0;y<ownerImage->height();y++)
            {
                int r=qRed(ownerImage->pixel(x,y));
                int g=qGreen(ownerImage->pixel(x,y));
                int b=qBlue(ownerImage->pixel(x,y));
                int y=( (  66 * r + 129 * g +  25 * b + 128) >> 8) +  16;
                int u=( ( -38 * r -  74 * g + 112 * b + 128) >> 8) + 128;
                int v=( ( 112 * r -  94 * g -  18 * b + 128) >> 8) + 128;
                histogramY[y]++;
                histogramU[u]++;
                histogramV[v]++;
            }
    }
}

QImage HistogramYUV::calcHistImage()
{
    int width=512;
    int height=300;
    int fracc=width/256;
    QImage histImage(width,height,QImage::Format_RGB32);
    unsigned long max=0;
    for(int i=0;i<256;i++)
    {
        if(histogramY[i]>max)
            max=histogramY[i];
        if(histogramU[i]>max)
            max=histogramU[i];
        if(histogramV[i]>max)
            max=histogramV[i];
    }
    for(int x=0;x<width;x++)
        for(int y=0;y<height;y++)
            histImage.setPixel(x,y,qRgb(255,255,255));
    for(int i=0;i<width;i++)
    {
        int hred=(height-1)*(1.0-histogramY[i/fracc]/(double)max);
        int hgreen=(height-1)*(1.0-histogramU[i/fracc]/(double)max);
        int hblue=(height-1)*(1.0-histogramV[i/fracc]/(double)max);
        for(int j=0;j<height;j++)
        {
            int r=0;
            int g=0;
            int b=0;
            if(j>hred)
            {
                r+=255;
                g+=96;
                b+=96;
            }
            if(j>hgreen)
            {
                r+=96;
                g+=255;
                b+=96;
            }
            if(j>hblue)
            {
                r+=96;
                g+=96;
                b+=255;
            }
            if(r>225) r=255;
            if(g>225) g=255;
            if(b>255) b=255;
            if(j==hred)
            {
                r=255;
                g=0;
                b=0;
            }
            if(j==hgreen)
            {
                r=0;
                g=255;
                b=0;
            }
            if(j==hblue)
            {
                r=0;
                g=0;
                b=255;
            }
            histImage.setPixel(i,j,qRgb(r,g,b));
        }
    }
    return histImage;
}
